package server;

import java.util.ArrayList;
import java.util.List;

import api.Person;

/**
 * @author Ann Raboschuk
 *
 */
public class ServerMOCK implements IAmServer
{
   private static final long serialVersionUID = 1L;
   private List<Person> persons = new ArrayList<Person>();
   
   public ServerMOCK()
   {
      persons.add(new Person("Jhons","Ann",21,'F',"mira-girl@yandex.ru","075261827","Kiev, Kirova st. 45"));
      persons.add(new Person("Prinston","Caren",25,'F',"mail@yandex.ru","37593759537","Dnipropetrivsk, Dimitrov st. 44"));
      persons.add(new Person("Garcia","Andrew",42,'M',"hvost@yandex.ru","34893245893","Donetsk, Lviv st. 44"));
      persons.add(new Person("Falk","Peter",65,'M',"helenitive@yandex.ru","084803850","Lviv, Gagarin st. 47"));
      for (int i=0; i<persons.size(); i++)
         persons.get(i).setId(new Long(i+1));
   }
   
   @Override
   public Long add(Person person)
   {
      persons.add(person);
      persons.get(persons.size()-1).setId(new Long(persons.size()));
      return  0l;
   }

   @Override
   public void delete(Person person)
   {
      persons.remove(person);
   }

   @Override
   public void edit(Person person)
   {
      for (int i=0; i<persons.size(); i++)
      {
         if (persons.get(i).getId().equals(person.getId()))
         {
            persons.set(i, person);
            break;
         }
      }
   }

   @Override
   public List<Person> loadAll()
   {
      return persons;
   }

   @Override
   public Person getPersonById(Long id)
   {
      Person selected = null;
      for (Person person: persons)
         if (person.getId().equals(id))
            selected = person;
      return selected;
   }
}
